/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/
package cn.com.dhc.danlu.fragment;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.fragment.base.BaseBridgeFragment;
import cn.com.dhc.danlu.fragment.bridge.FragmentBridge;
import cn.com.dhc.danlu.widget.CustomItemLayout;
import cn.com.hd.mall.web.webservices.entity.response.mydl.account.GetAcctInfoResponse;

/**
 * @author: 吴海洋
 * @author: 李一方
 * @version: 1.0.0
 * @since: 2016-11-07 04:30
 * @mail: wuhaiyang@danlu.com
 * @desc: 账号信息界面
 */
public class AccountInfoFragment extends BaseBridgeFragment {
    private boolean isActive = false;
    private CustomItemLayout mil_account;
    private CustomItemLayout mil_name;
    private CustomItemLayout mil_email;
    private CustomItemLayout mil_job;
    private TextView tv_phone;
    private TextView tv_tips;
    private RelativeLayout rl_active_status; // click
    private TextView tv_no_phone_tips;

    public boolean isActive () {
        return isActive;
    }

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate (R.layout.dl_fragment_accountinfo, container, false);
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        super.onViewCreated (view, savedInstanceState);
        mil_name = (CustomItemLayout) view.findViewById (R.id.mil_name);
        mil_account = (CustomItemLayout) view.findViewById (R.id.mil_account);
        mil_email = (CustomItemLayout) view.findViewById (R.id.mil_email);
        mil_job = (CustomItemLayout) view.findViewById (R.id.mil_job);
        tv_phone = (TextView) view.findViewById (R.id.tv_phone);
        tv_tips = (TextView) view.findViewById (R.id.tv_tips);
        rl_active_status = (RelativeLayout) view.findViewById (R.id.rl_active_status);

        tv_no_phone_tips = (TextView) view.findViewById (R.id.tv_no_phone_tips);
        bindAction ();
    }

    @Override
    public void onStart () {
        super.onStart ();
        // 界面可见的时候 请求服务器获取当前用户信息并且刷新当前界面
        mBridgeCallback.onHandle (FragmentBridge.TYPE_GET_ACCOUNT_INFO, null);
    }

    private void bindAction () {
        rl_active_status.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View v) {
                mBridgeCallback.onHandle (FragmentBridge.TYPE_PHONE_ACTION, null);
            }
        });
    }

    public void refreshInfo (GetAcctInfoResponse acctInfo) {
        mil_account.refreshTipText (acctInfo.getUserAcct ());
        mil_name.refreshTipText (acctInfo.getUserName ());
        mil_email.refreshTipText (acctInfo.getEmail ());
        mil_job.refreshTipText (acctInfo.getPosition (), 10);
        if (TextUtils.isEmpty (acctInfo.getTelStatus ()) || acctInfo.getTelStatus ().equals ("1")) {
            isActive = false;
        } else {
            isActive = acctInfo.getTelStatus ().equals ("0");
        }
        refreshActiveUI (isActive, acctInfo.getTel ());
    }

    public void refreshActiveUI (boolean isActive, String phone) {
        this.isActive = isActive;
        if (isActive) {
            // 已经激活了
            tv_tips.setVisibility (View.VISIBLE);
            tv_phone.setVisibility (View.VISIBLE);
            tv_no_phone_tips.setVisibility (View.GONE);
            String text = phone + "(已激活)";
            dividerShowTextStyle (tv_phone, text, text.indexOf ("("), text.length ());
            tv_tips.setText (R.string.has_active_tips);
        } else {
            if (!TextUtils.isEmpty (phone)) {
                tv_tips.setVisibility (View.VISIBLE);
                tv_phone.setVisibility (View.VISIBLE);
                tv_no_phone_tips.setVisibility (View.GONE);
                tv_tips.setText (getResources ().getString (R.string.active_tips));
                String text = phone + "(未激活)";
                dividerShowTextStyle (tv_phone, text, text.indexOf ("("), text.length ());
            } else {
                tv_tips.setVisibility (View.GONE);
                tv_phone.setVisibility (View.GONE);
                tv_no_phone_tips.setVisibility (View.VISIBLE);
                tv_no_phone_tips.setText (getResources ().getString (R.string.active_tips));
            }
        }
    }

    private void dividerShowTextStyle (TextView textView, String text, int startIndex, int endIndex) {
        Spannable textSpan = new SpannableStringBuilder (text);
        ForegroundColorSpan redSpan = new ForegroundColorSpan (getResources ().getColor (R.color.color_f2683a));
        textSpan.setSpan (redSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText (textSpan);
    }

    @Override
    public void onDestroy () {
        super.onDestroy ();
    }
}